//获取应用实例
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
Page({
	data: {
    jdList:[],
    zhuanMoney:0,
  },


  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  onReady(){
    this.getDeviceInfo();
  },
	onLoad: function(options) {
    var that=this;
    that.getRate();
    that.setData({
      jdList: app.globalData.jdList
    })
    var jlmoney1;
    var jlmoney2;
    var jlmoneysum;
    jlmoney1 = that.data.jdList.commissionInfo.commissionShare / 100;
    jlmoney2 = that.data.jdList.commissionInfo.plusCommissionShare / 100;
    jlmoneysum = that.data.jdList.commissionInfo.couponCommission;
    jlmoneysum = jlmoneysum - jlmoney1 - jlmoney2;
    that.setData({
      // jlmoneysum: jlmoneysum
    })
    console.log(that.data.jdList)
    // that.is_collect();
  },

  onShow:function(){
    var that=this;
  },


  //分享
  getQuan: function () {
    var that = this;
    wx.navigateToMiniProgram({
      appId: 'wx91d27dbf599dff74',
      path: 'pages/item/detail/detail?sku=' + that.data.jdList.skuId,
      success(res) {
        console.log(res)
      }
    })
  },
  
  /**
      * 滑动切换tab
      */
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });

  },

  /**
   * 点击tab切换
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  
  //预览图片
  previewImage: function (e) {
    var imgUrl = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  //查看图文详情
  toContent:function(e){
    console.log(e)
    wx.navigateTo({
      url: 'webview?url='+e.currentTarget.dataset.url,
    })
  },


  //获取奖金比例
  getRate: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=getCommissionRate',
      data: {
        token: app.globalData.token,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 0) {
          that.setData({
            zhuanMoney: (that.data.jdList.priceInfo.lowestCouponPrice * ((that.data.jdList.commissionInfo.commissionShare) / 100).toFixed(1)*
              (res.data.data.commission_rate)/100).toFixed(2)
          })
          console.log(that.data.jdList.priceInfo.lowestCouponPrice)
          console.log((that.data.jdList.commissionInfo.commissionShare) / 100)
          console.log((71) / 100)
          console.log(that.data.zhuanMoney)
        }
      }
    })
  },

  //用户是否收藏商品
  is_collect: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=JingdongCollect&a=is_collect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.jdList.skuId,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==0){
          if(res.data.data.is_collect=='Y'){
            that.setData({
              is_collect: true
            })
          }else{
            that.setData({
              is_collect: false
            })
          }
        }
      }
    })
  },

  //收藏商品
  collect: function () {
    var that = this;
    if(app.globalData.token!=""){
      if (that.data.is_collect == true) {
        that.cancel();
      } else {
        that.saveCollect();
      }
    }else{
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }
  },

  saveCollect:function(){
    var that=this;
    console.log(that.data.jdList.skuId)
    wx.request({
      url: app.globalData.api + '/app.php?c=JingdongCollect&a=collect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.jdList.skuId,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'post',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 0) {
          that.setData({
            is_collect:true
          })
        }else{
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //取消收藏
  cancel: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=TbGoodsCollect&a=cancelCollect',
      data: {
        token: app.globalData.token,
        goods_id: that.data.goods_id,
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if (res.data.code == 0) {
          that.setData({
            is_collect: false
          })
          // wx.showToast({
          //   title: '' + res.data.msg,
          //   icon: 'success',
          //   duration: 1000
          // })
        }else{
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  filter: function (e) {
    app.globalData.sku = this.data.skulist[e.currentTarget.dataset.index]
    // console.log(app.globalData.sku)
    var self = this, id = e.currentTarget.dataset.id, txt = e.currentTarget.dataset.txt, price = e.currentTarget.dataset.price
    self.setData({
      gg_id: id,
      gg_txt: txt,
      // gg_price: price
    });
  },

  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },


  //确定按钮
  cartModel:function(){
    this.hideModal();
    // this.submitOrder();
  },

  //显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  shopCart:function(){
    if(app.globalData.token!=''){
      wx.navigateTo({
        url: 'shop-cart',
      })
    }else{
      wx.reLaunch({
        url: 'login',
      })
    }
    
  },
  
});
